package com.hqyj.controller;


import com.hqyj.dao.EmployeeContractMapper;
import com.hqyj.pojo.EmployeeContract;
import com.hqyj.pojo.EmployeeFile;
import com.hqyj.service.IEmployeeContractService;
import com.hqyj.service.IEmployeeFileService;
import com.hqyj.util.ResponseUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rock
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/employeecontract")
public class EmployeeContractController {
    @Autowired
    IEmployeeContractService iEmployeeContractService;

    //加载表格数据
    @RequestMapping("/contractList")
    public HashMap<String,Object> listPage(EmployeeContract e){
        return iEmployeeContractService.select(e);
    }
    //修改
    @RequestMapping("/update")
    public HashMap<String,Object>  update(EmployeeContract e){
        return iEmployeeContractService.update(e);
    }
    //新增
    @RequestMapping("/add")
    public HashMap<String,Object> add(EmployeeContract e){
        return iEmployeeContractService.add(e);
    }

    //删除
    @RequestMapping("/del")
    public HashMap<String,Object> del(Integer id){
        return iEmployeeContractService.del(id);
    }

    //导出Excel
    @RequestMapping("/excelDown")
    public String excelDown(HttpServletResponse response,HttpServletRequest request,EmployeeContract e) throws Exception{
        String webPath = request.getServletContext().getRealPath("/");

        List<EmployeeContract> list =  iEmployeeContractService.selectAll(e);
        Workbook wb = fillExcelDataWithTemplate(list, webPath+"/static/client_down_model.xls");

        ResponseUtil.export(response,wb,"员工合同.xls");

        return null;
    }
    public static Workbook fillExcelDataWithTemplate(List<EmployeeContract> list ,String templateFileUrl) {
        Workbook wb = null ;
        try {
            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(templateFileUrl));
            wb = new HSSFWorkbook(fs);
            // 取得 模板的 第一个sheet 页
            Sheet sheet = wb.getSheetAt(0);
            // 拿到sheet页有 多少列
            int cellNums = sheet.getRow(0).getLastCellNum();
            // 从第2行 开搞    下标1  就是第2行
            int rowIndex = 1;
            Row row ;
            for(EmployeeContract e : list){
                row = sheet.createRow(rowIndex);
                rowIndex ++;
                row.createCell(0).setCellValue(e.getContractId());
                row.createCell(1).setCellValue(e.getEmployeeId());
                row.createCell(2).setCellValue(e.getcontractStartDate());
                row.createCell(3).setCellValue(e.getcontractEndDate());
                row.createCell(4).setCellValue(e.getPost());
                row.createCell(5).setCellValue(e.getContractContent());
                row.createCell(6).setCellValue(e.getRemarks());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
}



